package com.dameng.system.mapper;

import com.dameng.system.entity.SystemMenu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SystemMenuMapper extends BaseMapper<SystemMenu> {

    /**
     * 查询用户一级菜单
     * @param userId
     * @return
     */
    @Results({
            @Result(id=true,column="menuId",property="menu_id"),
            @Result(column="pid",property="pid"),
            @Result(column="name",property="name"),
            @Result(column="urlRequest",property="url_request"),
            @Result(column="menuStyle",property="menu_style")
    })
    @Select("select m.menu_id, m.pid, m.name, m.url_request, m.menu_style " +
            "from system_menu m where m.menu_id in " +
            "(select distinct rm.menu_id from system_role_menu rm inner join system_user_role sr " +
            "on rm.role_id = sr.role_id where sr.user_id=#{userId}) " +
            "and m.pid = '1' and status = '1' order by m.sort_no")
    List<SystemMenu> getMenuByUserId(String userId);

    /**
     * 查询用户二级及以下菜单
     * @param userId
     * @param menuId
     * @return
     */
    @Results({
            @Result(id=true,column="menuId",property="menu_id"),
            @Result(column="pid",property="pid"),
            @Result(column="name",property="name"),
            @Result(column="urlRequest",property="url_request"),
            @Result(column="menuStyle",property="menu_style")
    })
    @Select("select m.menu_id, m.pid, m.name, m.url_request, m.menu_style " +
            "from system_menu m where m.menu_id in " +
            "(select distinct rm.menu_id from system_role_menu rm inner join system_user_role sr " +
            "on rm.role_id = sr.role_id where sr.user_id=#{userId}) and status = '1' " +
            "start with m.pid=#{menuId}" +
            "connect by prior m.menu_id = m.pid order by m.sort_no")
    List<SystemMenu> childMenuByUserId(String userId, String menuId);

    @Results({
            @Result(id=true,column="menuId",property="menu_id"),
            @Result(column="pid",property="pid"),
            @Result(column="name",property="name"),
            @Result(column="checked",property="checked")
    })
    @Select("select m.menu_id, m.pid, m.name, " +
            "case (select count(1) from system_role_menu where role_id = rm.role_id) when 0 then false " +
            "else true end checked " +
            "from system_menu m left join system_role_menu rm on m.menu_id = rm.menu_id and rm.role_id=#{roleId}")
    List<SystemMenu> getMenusByRoleId(String roleId);

    @ResultType(List.class)
    @Select("select menu_id " +
            "from system_menu " +
            "start with pid=#{menuId} " +
            "connect by prior menu_id = pid")
    List<String> childMenuByMenuId(String menuId);
}
